// Learn cc.Class:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/class.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/class.html
// Learn Attribute:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - [Chinese] http://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
//  - [English] http://www.cocos2d-x.org/docs/creator/en/scripting/life-cycle-callbacks.html
let panel = require("panelBase");
let viewConfig = {
    btn_wxLogin:{
        node:null,
        name:"loginBg/btn_wxLogin",
        eventType:"click",
        cb:"onBtnLoginClick",
       // active:false,
    },

    check_tog:{
        node:null,
        name:"content/check",
        eventType:"toggle",
        cb:"onToggle"
    },
    btn_agreement:{
        node:null,
        name:"content/btn_agreement",
        eventType:"click",
        cb:"onBtnAgreementClick"
    },
    btn_privacy:{
        node:null,
        name:"content/btn_privacy",
        eventType:"click",
        cb:"onBtnPrivacyClick"
    },


}
cc.Class({
    extends: panel,

    properties: {
        isAgree:true,
    },
    onLoad() {
        this.init(viewConfig);
       
        cc.tg = {};
        let ec = require("cusEventCenter");
        cc.tg.eventCenter = new ec();
        cc.tg.eventCenter.init();
        let http = require("http")
        cc.tg.http = new http();
        cc.tg.http.initConf("49.235.76.154", "8080");
        let audioMgr = require("audioMgr");
        cc.tg.audioMgr = new audioMgr();
        cc.tg.audioMgr.setVolume();

        let adDataCenter = require("AdDataCenter")
        cc.tg.adDataCenter = new adDataCenter();
        cc.tg.adDataCenter.init();

        // cc.director.preloadScene("mainScene",()=>{
        //     cc.log("preload senene done");
        // })
        //  cc.tg.audioMgr.play("leshphon.mp3",true);
        // cc.tg.eventCenter.addEvent("test",(data)=>{
        //     cc.log(data);
        // });


        let jcj = require("JavaCallJS")
        cc.tg.jcj = new jcj();


        let gameLogic = require("httpGameLogic")
        cc.tg.gameLogic = new gameLogic();
       
        let cakeRoom = require("cakeRoom")
        cc.tg.cakeRoom = new cakeRoom();
        cc.tg.cakeRoom.init();


       let check =  viewConfig["check_tog"].node.getComponent(cc.Toggle);
       check.isChecked = true; 
        this.isAgree = true;

        cc.sys.localStorage.setItem("wxAppId",'')
        let appId = cc.sys.localStorage.getItem("wxAppId");
        if(!appId)
        {
            cc.sys.localStorage.setItem("wxAppId","appId_dg")
            appId =  window.localStorage.getItem("wxAppId");
        }
        if(cc.sys.isBrowser)
        {
          //  viewConfig["btn_wxLogin"].node.active = false;
          if(cc.tg.cakeRoom.isDebug)
          {
              cc.tg.jcj.onSplashAdOver();
          }
          else
          {
            cc.tg.gameLogic.toLogin("0",appId);
          }

        }
        else
        {
            let code = cc.sys.localStorage.getItem("wxCode");
            
            if(!code || !appId)
            {
                viewConfig["btn_wxLogin"].node.active = true;
            }
            else
            {
               // this.toLogin(code,appId);
               cc.tg.gameLogic.toLogin(code,appId);
            }
        }

    },
    init() {
        this._super(viewConfig);
        this.isInited = true;
    },


    onBtnLoginClick() {
        //cc.director.loadScene("mainScene");
        if(!this.isAgree)
        {
            cc.log("未同意用户协议或者隐私政策");
            return ;
        }
        cc.tg.adDataCenter.wxLogin();
    },

    onDestroy() {
        for (let key in viewConfig) {
            viewConfig[key].node = null;
        }
    },

    onToggle(){
        this.isAgree = !this.isAgree;
    },

    onBtnAgreementClick(){

    },

    onBtnPrivacyClick(){

    },

    


});
